package com.njegovanobradovic.icet.pages;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Grid;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

import com.njegovanobradovic.icet.facade.Controller;
import com.njegovanobradovic.icet.metamodel.Chat;
import com.njegovanobradovic.icet.metamodel.Room;
import com.njegovanobradovic.icet.metamodel.User;

@Import(library = "js/AJAXPeriodicUpdate.js")
public class Home {
	@Inject
	private Controller controller;
	
	
	@Property
	private User user;
	@InjectPage
	private Details detailsPage;
	@InjectPage
	private HistoryChat historyChat;

	
	@Property
	private Chat chat;
	@InjectPage
	private Chating chating;
	
	
	public Object onActivate() {
		return controller.isLoggedIn() ? null : Signin.class;
	}


	public List<User> getLoggedUsers() {
		List<User> list = new LinkedList<User>();
		for (User u : controller.getAllUsers()) {
			if (u.isStatus()==true) {
				list.add(u);
			}
		}
		return list;
	}
	@OnEvent(component = "detailsLink")
	Object onShowDetails(String username) {
		detailsPage.setUser(controller.getUser(username));
		return detailsPage;
	}
	
	@OnEvent(component = "chatLink")
	Object onShowChat(String roomName) {
		Chat chat = controller.getChat(roomName);
		chat.setUsers(controller.getChatUsers(chat));
		chat.setMessages(controller.getChatMessages(chat));
		chating.setChat(chat);
		return chating;
	}
	
	@OnEvent(component = "chatLink2")
	Object onShowChat2(String roomName) {
		Chat chat = controller.getChat(roomName);
		chat.setUsers(controller.getChatUsers(chat));
		chat.setMessages(controller.getChatMessages(chat));
		historyChat.setChat(chat);
		return historyChat;
	}
	
	public List<Chat> getChats(){
		return controller.getChatsOfUser(controller.getLoggedUser());
	}
	public List<Room> getRooms(){
		List<Room> rooms = new LinkedList<Room>();
		for (Chat c : getChats()) {
			rooms.add(c.getRoom());
		}
		Comparator<Room> com = Collections.reverseOrder();
		Collections.sort(rooms, com);
		return rooms;
	}
	
	public String getFirstMessage(){
		if (controller.getChatUsers(chat).get(0).equals(controller.getLoggedUser())) {
			return chat.getDate()+"- You are waiting '"+controller.getChatUsers(chat).get(1).getName()+"' to answer!";
		}
		return chat.getDate()+"- '"+controller.getChatUsers(chat).get(0).getName()+"' send you a message!";
	}
	
	
	@InjectComponent
	private Zone listZone;
	@Inject
	private JavaScriptSupport javaScriptSupport;
	
	public void setupRender() {
		javaScriptSupport.addInitializerCall("periodicTimeZoneUpdater", new JSONObject());
	}
	
	Object onRefreshTimeZone() {
		return !controller.getRequest().getSession(true).isInvalidated() ? listZone.getBody() : controller.userLogout();
	}
	
	
	@InjectComponent
	private Grid grid;
	@Property
	private Room room;
	@Log
	void onActionFromDelete(String name)
    {
		if (controller.getChat(name).isReceivedMessage()) {
			controller.removeChatFromChatList(controller.getLoggedUser(), controller.getChat(name));
		}
    }
}
